<?php

namespace app\api\controller;

use app\api\model\Team as TeamModel;
use app\common\model\team\Category as CategoryModel;
use think\Db;

/**
 * 服务队控制器
 * Class Team
 * @package app\api\controller
 */
class Team extends Controller
{
    /**
     *
     * @return array
     */
    public function index()
    {
        // 服务队分类列表
        $categoryList = CategoryModel::getAll();
        return $this->renderSuccess(compact('categoryList'));
    }

    /**
     * 服务队列表
     * @param int $category_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function lists($area_id = 0, $searchInput = "")
    {
        $model = new TeamModel;
        $user = $this->getUser();
        $shanghui_id = $user["shanghui_id"];//当前用户的商会id
        $list = $model->getList($area_id, $searchInput, 10000, $user['team_id'],$shanghui_id);
        return $this->renderSuccess(compact('list'));
    }

    /**
     * 服务队详情
     * @param $team_id
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function detail($team_id)
    {
        $detail = TeamModel::detail($team_id);
        return $this->renderSuccess(compact('detail'));
    }

    /**
     * 服务队积分排行
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function pointsRanking()
    {
        // 当前用户信息
        $user = $this->getUser();
        $shanghui_id = $user["shanghui_id"];//当前用户的商会id
        //$user = Db::name('user')->find(10177);
        $points_ranking = Db::name('team')->where('wxapp_id', $this->wxapp_id)->where('shanghui_id', $shanghui_id)->order(['points' => 'desc', 'team_id' => 'ASC'])->limit(100)->select()->toArray();
        $my_team = [];
        foreach ($points_ranking as $k => &$v) {
            $image = Db::name('upload_file')->find($v['image_id']);
            $v['logo'] = $image['file_url'] . '/' . $image['file_name'];
            $v['ranking'] = $k + 1;
            if ($v['team_id'] == $user['team_id']) {
                $my_team['myteam_ranking'] = $k + 1;
                $my_team['myteam_points'] = $v['points'];
            }
        }
        return $this->renderSuccess(compact('points_ranking', 'my_team'));
    }

    /**
     * 服务队捐款排行
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function donateRanking()
    {
        // 当前用户信息
        $user = $this->getUser();
        $shanghui_id = $user["shanghui_id"];//当前用户的商会id
        $points_ranking = Db::name('team')->where('wxapp_id', $this->wxapp_id)->where('shanghui_id', $shanghui_id)->order(['donate' => 'desc', 'team_id' => 'ASC'])->limit(1000)->select()->toArray();
        $my_team = [];
        foreach ($points_ranking as $k => &$v) {
            $image = Db::name('upload_file')->find($v['image_id']);
            $v['logo'] = $image['file_url'] . '/' . $image['file_name'];
            $v['ranking'] = $k + 1;
            if ($v['team_id'] == $user['team_id']) {
                $my_team['myteam_ranking'] = $k + 1;
                $my_team['myteam_donate'] = $v['donate'];
            }
        }
        return $this->renderSuccess(compact('points_ranking', 'my_team'));
    }


}
